package com.notes.action;
import java.util.Map;

import javax.faces.application.FacesMessage;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import org.primefaces.context.RequestContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;

import com.notes.service.UserService;


@Component(value="userAddAction")
@SessionScoped
public class UserAddAction {
	
	private String userName;
	private String password;
	private String confirmPassword;
	
//	Map session = (Map) ActionContext.getContext().get("session");
	
	@Autowired
	private UserService userService;
	
	public UserAddAction(){
		
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getConfirmPassword() {
		return confirmPassword;
	}
	public void setConfirmPassword(String confirmPassword) {
		this.confirmPassword = confirmPassword;
	}


	
	public String userAdd(){
		if(!password.equals(confirmPassword)){
			FacesContext.getCurrentInstance().addMessage("userAddMsg", new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error! ", "Passwords are different"));
			return null;
		}
		else{
			Long userId = (Long)userService.addUser(userName,password);
			if(userId==null){
				FacesContext.getCurrentInstance().addMessage("userAddMsg", new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error! ", "User is already existed"));
				return null;
			}
		}
		return "webPages/success?faces-redirect=true";
	}
	
	public void userAddDialog(){
		RequestContext.getCurrentInstance().openDialog("jh");
	}
	
	
}
